package com.zisu.rabbitconsumer.component.equipment;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

/**
 * @Desc: ""
 * @Author: caixiang
 * @DATE: 2021/6/22 15:36
 */
@Component
public class DeadLetterQueueReceived {
    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(value="Dead_Letter_Queue_CSS",durable = "true"),
                    exchange = @Exchange(name = "Dead_Letter_Exchange",durable = "true",type = ExchangeTypes.DIRECT,ignoreDeclarationExceptions = "true"),
                    key = "Dead_Letter_Queue_CSS_RoutingKey"
            )
    )
    @RabbitHandler
    public void dlForCSS(Message<?> message, Channel channel)throws Exception{

        System.out.println("线程名"+Thread.currentThread().hashCode()+"==============Dead_Letter_Exchange - Dead_Letter_Queue_CSS=================,priority:"+message.getHeaders().get("priority")+",attr1"+message.getHeaders().get("attr1"));
        Thread.sleep(5000);
        Long deliveryTag = (Long)message.getHeaders().get(AmqpHeaders.DELIVERY_TAG);

        //throw new Exception("12331");

        //channel.basicNack(deliveryTag,false,true);
        channel.basicAck(deliveryTag,false);
    }

    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(value="Dead_Letter_Queue_Acoat",durable = "true"),
                    exchange = @Exchange(name = "Dead_Letter_Exchange",durable = "true",type = ExchangeTypes.DIRECT,ignoreDeclarationExceptions = "true"),
                    key = "Dead_Letter_Queue_Acoat_RoutingKey"
            )
    )
    @RabbitHandler
    public void dlForAcoat(Message<?> message, Channel channel)throws Exception{

        System.out.println("线程名"+Thread.currentThread().hashCode()+"==============Dead_Letter_Exchange - Dead_Letter_Queue_Acoat=================,priority:"+message.getHeaders().get("priority")+",attr1"+message.getHeaders().get("attr1"));
        Thread.sleep(5000);
        Long deliveryTag = (Long)message.getHeaders().get(AmqpHeaders.DELIVERY_TAG);

        //throw new Exception("12331");

        //channel.basicNack(deliveryTag,false,true);
        channel.basicAck(deliveryTag,false);
    }
}
